package com.sjm.bookshop.controller.slide;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjm.bookshop.entity.Slide;
import com.sjm.bookshop.service.slide.SlideService;
import com.sjm.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/slide")
public class SlideController {

    @Autowired
    private SlideService slideService;

    //轮播列表
    @RequestMapping("/selectAllSlide")
//    @ResponseBody
    public Result selectAll(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Slide> slides = slideService.slideList();
        PageInfo<Slide> pageInfo = new PageInfo<>(slides);

        return Result.success(200, pageInfo, "success");
    }

    //修改状态
    @RequestMapping("/change")
//    @ResponseBody
    public Result change(int slideId, int slidestatus) {
        if (slideId >= 0 && slidestatus >= 0) {
            int i = slideService.changeStatus(slideId, slidestatus);
            if (i > 0) {
                return Result.success(200, "success");
            } else {
                return Result.fail(201, "fail");
            }
        } else {
            return Result.fail(201, "fail");
        }

    }

    //搜索
    @RequestMapping("/selectByKeyWord")
//    @ResponseBody
    public Result select(int page, int limit, String keyWord) {
        if (page >= 0 && limit >= 0 && (keyWord != null || keyWord.equals(" "))) {
            PageHelper.startPage(page, limit);
            List<Slide> slides = slideService.slideListByLike(keyWord);
            PageInfo<Slide> pageInfo = new PageInfo<>(slides);
            if (pageInfo.getList().size() > 0) {
                return Result.success(200, pageInfo.getList(), "success");
            } else {
                return Result.fail(201, "fail");
            }
        } else {
            return Result.fail(201, "fail");
        }
    }


    //删除
    @RequestMapping("/deleteOne/{slideId}")
//    @ResponseBody
    public Result deleteOne(@PathVariable("slideId") int slideId) {
        if (slideId > 0) {
            if (slideService.delete(slideId) > 0) {
                return Result.success(200, "success");
            } else {
                return Result.fail(201, "fail");
            }
        } else {
            return Result.fail(201, "fail");
        }
    }

    // 添加轮播
    @RequestMapping("/addSlide")
//    @ResponseBody
    public Result addSlide(@RequestBody Slide slide) {
        if (slide != null && !slide.equals(null)) {
            int res = slideService.addOneSlide(slide);
            if (res > 0) {
                return Result.success(200, "success");
            } else {
                return Result.fail(201, "添加失败");
            }
        } else {
            return Result.fail(201, "表单不能为空");
        }
    }

    // 修改轮播
    @RequestMapping("/updateSlide")
//    @ResponseBody
    public Result updateSlide(@RequestBody Slide slide) {
        if (slide != null && !slide.equals(null)) {
            int res = slideService.UpdateOneSlide(slide);
            if (res > 0) {
                return Result.success(200, "success");
            } else {
                return Result.fail(201, "添加失败");
            }
        } else {
            return Result.fail(201, "表单不能为空");
        }
    }


}
